package com.DHH.Controller.map;

import com.DHH.Dto.FilterQuery;
import com.DHH.Dto.Series;
import com.DHH.Dto.StatisticsEntity;
import com.DHH.Entity.Statistics;
import com.DHH.Mapper.Public.StatisticsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/Admin/Statistics")
public class StatisticsController {
    @Autowired
    private StatisticsMapper statisticsMapper;
    @RequestMapping("/statisticsPage")
    public String StatisticsPage(ModelMap modelMap)
    {
        modelMap.put("todayUser",statisticsMapper.selectTodayUser());
        modelMap.put("todayOrder",statisticsMapper.selectTodayOrder());
        modelMap.put("sumOrder",statisticsMapper.selectSumOrder());
        Float sumProfit=statisticsMapper.selectSumProfit();
        Float sumSecurity=statisticsMapper.selectSumSecurity();
        Float sumSurplusMoney=statisticsMapper.selectSumSurplusMoney();
        Float todayProfit=statisticsMapper.selectTodayProfit();
        if(sumProfit==null)sumProfit=0f;
        if(sumSecurity==null)sumSecurity=0f;
        if(sumSurplusMoney==null)sumSurplusMoney=0f;
        if(todayProfit==null)todayProfit=0f;
        modelMap.put("sumUser",statisticsMapper.selectSumUser());
        modelMap.put("todayProfit",todayProfit);
        modelMap.put("sumProfit",sumProfit);
        modelMap.put("sumSecurity",sumSecurity);
        modelMap.put("sumSurplusMoney",sumSurplusMoney);
        return "admin/home.jsp";
    }
    @RequestMapping("/statisticsEntity")
    @ResponseBody
    public StatisticsEntity statisticsEntity(FilterQuery filterQuery) throws ParseException {
       System.out.println(filterQuery);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        DateFormat ssdf = new SimpleDateFormat("MM-dd");
        if(filterQuery.getStartValue()==null)
        {
            filterQuery.setStartValue(sdf.format(new Date(new Date().getTime() -(24 * 3600 * 1000)*15)));
        }
        if(filterQuery.getEndValue()==null)
        {
            filterQuery.setEndValue(sdf.format(new Date().getTime()+24 * 3600 * 1000));
        }
        filterQuery.setEndValue(sdf.format(new Date(sdf.parse(filterQuery.getEndValue()).getTime() + 24 * 3600 * 1000)));
        List<Statistics> userList=statisticsMapper.selectUser(filterQuery);
        List<Statistics> orderList=statisticsMapper.selectOrder(filterQuery);
        List<Statistics> profitList=statisticsMapper.selectProfit(filterQuery);
        Long time = sdf.parse(filterQuery.getStartValue()).getTime();
        Long endTime=sdf.parse(filterQuery.getEndValue()).getTime();
        Long oneDay= 24 * 3600 * 1000L;
        StatisticsEntity statisticsEntity=new StatisticsEntity();
        statisticsEntity.setTitle("数量统计报表");
        List<String> categoriesList=new ArrayList<>();
        Series seriesUser=new Series();
        seriesUser.setName("用户");
        seriesUser.setData(new ArrayList<>());
        Series seriesOrder=new Series();
        seriesOrder.setName("订单");
        seriesOrder.setData(new ArrayList<>());
        Series seriesProfit=new Series();
        seriesProfit.setName("收益");
        seriesProfit.setData(new ArrayList<>());
        while (time <= endTime) {
            Date d = new Date(time);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            categoriesList.add(ssdf.format(d));
            String strDate=df.format(d);
            //用户段
            Statistics statistics=checkInArray(strDate,userList);
            if(statistics!=null)
            {
                seriesUser.getData().add(statistics.getNumber());
            }else{
                seriesUser.getData().add(0);
            }
            //订单段
             statistics=checkInArray(strDate,orderList);
            if(statistics!=null)
            {
                seriesOrder.getData().add(statistics.getNumber());
            }else{
                seriesOrder.getData().add(0);
            }
             statistics=checkInArray(strDate,profitList);
            if(statistics!=null)
            {
                seriesProfit.getData().add(statistics.getNumber());
            }else{
                seriesProfit.getData().add(0);
            }
            time += oneDay;
        }
        statisticsEntity.setList(new ArrayList<Series>());
        statisticsEntity.getList().add(seriesUser);
        statisticsEntity.getList().add(seriesOrder);
        statisticsEntity.getList().add(seriesProfit);
        statisticsEntity.setCategories(categoriesList);
        return  statisticsEntity;
    }
    public Statistics checkInArray(String now,List<Statistics> statisticsList)
    {
        for (Statistics s:statisticsList)
        {
            if(s.getDate().equals(now))
            {
                return s;
            }
        }
        return null;
    }
}
